package com.prs.crm.service.assets.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.prs.crm.dao.assets.AssetsLingYongDao;
import com.prs.crm.domain.assets.AssetsLingYong;
import com.prs.crm.domain.assets.AssetsLiuXiang;
import com.prs.crm.domain.assets.AssetsRegister;
import com.prs.crm.domain.assets.LingYongHistory;
import com.prs.crm.service.assets.AssetsLingYongService;
import com.prs.crm.service.assets.AssetsLiuXiangService;
import com.prs.crm.service.assets.LingYongHistoryService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class AssetsLingYongServiceImpl extends BaseServiceImpl<AssetsLingYong, AssetsLingYongDao>
				implements AssetsLingYongService{
	private LingYongHistoryService lingYongHistoryService; //领用历史
	private LingYongHistory lingYong;
	private Set<AssetsRegister> regs;
	private AssetsLiuXiangService assetsLiuXiangService;
	public AssetsLiuXiangService getAssetsLiuXiangService() {
		return assetsLiuXiangService;
	}

	public void setAssetsLiuXiangService(AssetsLiuXiangService assetsLiuXiangService) {
		this.assetsLiuXiangService = assetsLiuXiangService;
	}

	public LingYongHistoryService getLingYongHistoryService() {
		return lingYongHistoryService;
	}

	public void setLingYongHistoryService(
			LingYongHistoryService lingYongHistoryService) {
		this.lingYongHistoryService = lingYongHistoryService;
	}

	public LingYongHistory getLingYong() {
		return lingYong;
	}

	public void setLingYong(LingYongHistory lingYong) {
		this.lingYong = lingYong;
	}
//////////////////////////////////////////////////////////////////////////
	@Override
	public List<AssetsLingYong> getAll() {
		// TODO Auto-generated method stub
		return this.getDao().getAllLingy();
	}

	@Override
	public void saveLingYongAndHitory(AssetsLingYong ly) {
		this.save(ly);
		lingYong = new LingYongHistory();
		lingYong.setSname(ly.getSname());
		lingYong.setSstate(ly.getSstate());
		lingYong.setNumber(ly.getNumber());
		lingYong.setLuser(ly.getLuser());
		lingYong.setLdep(ly.getLdep());
		lingYong.setLdate(ly.getLdate());		
		System.out.println(ly.getAssetsRegisters().size());
		if(ly.getAssetsRegisters().size() != 0 || ly.getAssetsRegisters() != null){
		   regs = new HashSet<AssetsRegister>();
			for(AssetsRegister reg : ly.getAssetsRegisters()){
				regs.add(reg);
			}
			lingYong.setAssetsRegisters(regs);
		}

		lingYong.setRemark(ly.getRemark());
		lingYong.setSpren(ly.getSpren());
		lingYong.setShuoming(ly.getShuoming());
		lingYong.setType(ly.getType());
		lingYong.setSpbz(ly.getSpbz());
		lingYong.setPerson(ly.getPerson());
		lingYong.setId(ly.getId());
		this.lingYongHistoryService.save(lingYong);
		
		AssetsLiuXiang liuxiang =new AssetsLiuXiang(); 
		liuxiang.setId(ly.getId());
		liuxiang.setLdep(ly.getLdep());
		liuxiang.setLuser(ly.getLuser());
		liuxiang.setDate(ly.getLdate());
		if (ly.getAssetsRegisters().size()!=0 || ly.getAssetsRegisters()!=null) {
			Set<AssetsRegister> assets=new HashSet<AssetsRegister>();
			for (AssetsRegister asset : ly.getAssetsRegisters()) {
				assets.add(asset);
			}
			liuxiang.setAssetsRegisters(assets);
		}
		liuxiang.setPerson(ly.getPerson());
		liuxiang.setType("领用");
		this.assetsLiuXiangService.save(liuxiang);
	}
}
